package com.ruoyi.zhoubao.tool;

// WpsSheetParser.java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.*;

/**
 * WPS在线表格解析工具类
 */
public class WpsSheetParser {

    private String accessToken;
    private static final String WPS_API_BASE_URL = "https://openapi.wps.cn";
    //private static final String WPS_API_BASE_URL = "https://www.kdocs.cn";

    /**
     * 构造函数
     * @param accessToken WPS API访问令牌
     */
    public WpsSheetParser(String accessToken) {
        this.accessToken = accessToken;
    }

    /**
     * 从WPS在线文档中下载并解析表格内容
     * @param fileId 文件ID
     * @return 解析后的表格数据
     * @throws IOException IO异常
     */
    public List<List<String>> parseOnlineSheet(String fileId) throws IOException {
        // 下载文件
        InputStream fileStream = downloadFile(fileId);

        // 解析表格内容
        return parseSheetContent(fileStream);
    }

    /**
     * 下载WPS在线文件
     * @param fileId 文件ID
     * @return 文件输入流
     * @throws IOException IO异常
     */
    private InputStream downloadFile(String fileId) throws IOException {
        String downloadUrl = WPS_API_BASE_URL + "/file/v1/download?file_id=" + fileId;
        System.out.println("downloadUrl:" +downloadUrl);
        URL url = new URL(downloadUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", "Bearer " + accessToken);

        if (connection.getResponseCode() != 200) {
            throw new IOException("Failed to download file: " + connection.getResponseMessage());
        }

        return connection.getInputStream();
    }

    /**
     * 解析表格内容
     * @param fileStream 文件输入流
     * @return 表格数据
     * @throws IOException IO异常
     */
    private List<List<String>> parseSheetContent(InputStream fileStream) throws IOException {
        List<List<String>> result = new ArrayList<>();
        Workbook workbook = WorkbookFactory.create(fileStream);

        try {
            Sheet sheet = workbook.getSheetAt(0); // 默认解析第一个工作表
            for (Row row : sheet) {
                List<String> rowData = new ArrayList<>();
                for (Cell cell : row) {
                    rowData.add(getCellValueAsString(cell));
                }
                result.add(rowData);
            }
        } finally {
            workbook.close();
            fileStream.close();
        }

        return result;
    }

    /**
     * 获取单元格值为字符串
     * @param cell 单元格对象
     * @return 单元格字符串值
     */
    private String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return "";
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }
}

